﻿using MesBSW.BLL.Role;
using MesBSW.DAL.RoleDB;
using MesBSW.Tools;
using MesBSW.View.Dialog.Role;
using Sunny.UI;
using System;
using System.Windows.Forms;

namespace MesBSW.View.RolePages
{
    public partial class MenuFrm : UIPage
    {
        private MenuControler _my_ctrl = new MenuControler();
        public MenuFrm()
        {
            InitializeComponent();
        }

        private void InitMenuTreeView()
        {
            this.treeViewMenu.Nodes.Clear();
            var menus = _my_ctrl.getMenuTreeNodes();
            TreeNode TopNode = new TreeNode("菜单", menus.ToArray());
            TopNode.Toggle();
            this.treeViewMenu.Nodes.Add(TopNode);
        }


        private void InitMenuDgv()
        {
            dgvMenu.Init();
            dgvMenu.AddColumn("ID", "ID");
            dgvMenu.AddColumn("menu_name", "menu_name").HeaderText = "菜单名称";
            dgvMenu.AddColumn("sort", "sort").HeaderText = "排序";
            dgvMenu.AddColumn("class_name", "class_name").HeaderText = "菜单路径";
            dgvMenu.AddColumn("create_user", "create_user").HeaderText = "创建人";
            InitMenuDatas();
        }

        private void InitMenuDatas()
        {
            dgvMenu.DataSource = _my_ctrl.getMenus().ToArray();
        }


        private void btnAddMenu_Click(object sender, EventArgs e)
        {
            AddMenuDia addMenuDia = new AddMenuDia(_my_ctrl);
            addMenuDia.Render();
            addMenuDia.ShowDialog();
            if (addMenuDia.IsOK)
            {
                RefreshData();
            }
        }



        private void treeViewMenu_AfterSelect(object sender, TreeViewEventArgs e)
        {
            dgvMenu.DataSource = _my_ctrl.getMenus(e.Node.Name.ToInt()).ToArray();
        }

        private void dgvMenu_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
        {
            BaseMenuModel menuModel = new BaseMenuModel();
            menuModel.id = dgvMenu.Rows[e.RowIndex].Cells["ID"].Value.ToString().ToInt();
            menuModel = _my_ctrl.getMenu(menuModel.id);
            AddMenuDia addMenuDia = new AddMenuDia(_my_ctrl, menuModel);
            addMenuDia.Render();
            addMenuDia.ShowDialog();
            if (addMenuDia.IsOK)
            {
                RefreshData();
            }
        }

        private void btndelMenu_Click(object sender, EventArgs e)
        {
            if (this.dgvMenu.SelectedRows.Count > 0)
            {
                int id = dgvMenu.Rows[dgvMenu.SelectedIndex].Cells["ID"].Value.ToString().ToInt();
                _my_ctrl.delMenus(id);
                RefreshData();
                UIMessageBox.Show(SysTools.errMsg);
            }
            else
            {
                UIMessageBox.ShowWarning("请在右边的列表中选择后再点击【删除】按钮！");
            }
        }
        private void btnRefresh_Click(object sender, EventArgs e)
        {
            RefreshData();
        }

        private void RefreshData() 
        {
            InitMenuTreeView();
            InitMenuDatas();

        }

        private void MenuFrm_Load(object sender, EventArgs e)
        {
            InitMenuTreeView();
            InitMenuDgv();
        }
    }
}
